#include "Brick.h"
#include "Resource.h"


Brick::Brick(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler, float x, float y, int ID, int Obj_state) : Object(x, y)
{
	this->setIDObjType(ID);

	if (this->getIDObjType() == 43)
	{
		p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[44]->getFileName()), 125, 0);
	}
	else if (this->getIDObjType() == 431)
	{
		p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[55]->getFileName()), 125, 0);
	}

	width = p_sprite->getTexture()->getWidth();
	height = p_sprite->getTexture()->getHeight();

	veloc_x = 0;
	veloc_y = 0;
 
	pos_x_old = x;
	pos_y_old = y;


	accel_y = 0;
	isBreaked = false;
	 
	_spriteArr[0] = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[43]->getFileName()), 125, 0);
	_spriteArr[1] = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[43]->getFileName()), 125, 0);
	_spriteArr[2] = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[43]->getFileName()), 125, 0);
	_spriteArr[3] = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[43]->getFileName()), 125, 0);

	this->obj_state = Obj_state;//BS_NORMAL;
	 
}

Brick::~Brick()
{
	/*if (_sprite != NULL)
	{
	delete _sprite;
	}*/
}


void Brick::render(int cameraX, int cameraY)
{
	switch(obj_state)
	{
	case BS_NORMAL:
		p_sprite->render(pos_x, pos_y, cameraX, cameraY);
		break;

	case BS_BREAKING:
		_spriteArr[0]->render(xBreak1, yBreak1, cameraX, cameraY); 
		_spriteArr[1]->render(xBreak2, yBreak2, cameraX, cameraY);
		_spriteArr[2]->render(2 * pos_x - xBreak1, yBreak1, cameraX, cameraY);
		_spriteArr[3]->render(2 * pos_x - xBreak2, yBreak2, cameraX, cameraY);
		break;
	}
}


void Brick::update(list<Object*>* obj_list, DxInput* input, float gameTime)
{
	list<Object*>::iterator i;
	Object* obj;

	switch(obj_state)
	{
	case BS_NORMAL:
		p_sprite->update(gameTime);
		break;

	case BS_BREAKING:
		xBreak1 += vxBreak1 * gameTime;
		xBreak2 += vxBreak2 * gameTime;

		vyBreak1 -= ayBreak * gameTime;
		vyBreak2 -= ayBreak * gameTime;

		yBreak1 += vyBreak1 * gameTime + 1.0f/2.0f * ayBreak * gameTime * gameTime;
		yBreak2 += vyBreak2 * gameTime + 1.0f/2.0f * ayBreak * gameTime * gameTime;

		if(yBreak1 <= 0)
		{
			changeState(BS_REMOVE);
		}

		_spriteArr[0]->update(gameTime);
		_spriteArr[1]->update(gameTime);
		_spriteArr[2]->update(gameTime);
		_spriteArr[3]->update(gameTime);

		break;
	}
}

void Brick::changeState(int state)
{
	obj_state = state;
	switch (obj_state)
	{
	case BS_NORMAL:
		break;
	
	case BS_BREAKING:
		xBreak1 = xBreak2 = pos_x + 40/2;
		yBreak1 = pos_y;
		yBreak2 = pos_y + 40;

		vyBreak1 = 1.0;
		vyBreak2 = 0.8;

		vxBreak1 = 0.150;
		vxBreak2 = 0.100;
		ayBreak = BRICK_GRAVITY;
		break;
	
	case BS_REMOVE:
		vxBreak1 = vxBreak2 = 0;
		vyBreak1 = vyBreak2 = 0;
		ayBreak = 0;
		break;
	}
}

OBJECT_TYPE Brick::getObjectType()
{
	return OBJECT_LAND;
}